<?php

include "DBConnection.php";
include "model/Car.php";
include "model/Track.php";
include "model/TrackData.php";
include "model/User.php";

$connection = new DBConnection();
$connection -> connect();

$trackID = $_REQUEST['trackID'];

if (is_numeric($trackID)) {
	$statement = $connection -> query("
		SELECT track.id, track.date, user.id as userID, user.name as userName, 
		car.id as carID, car.name as carName, car.horsepower as carHorsePower, 
		track_data.id as trackDataID, track_data.lat as lat, track_data.lon as lon, 
		track_data.alt as alt, track_data.consumption as consumption, 
		track_data.speed as speed, 
		track_data.engineLoad as engineLoad, 
		track_data.engineCoolantTemperature as engineCoolantTemperature, 
		track_data.engineRuntime as engineRuntime, 
		track_data.airTemperature as airTemperature, 
		track_data.oilTemperature as oilTemperature, 
		track_data.fuelRate as fuelRate, track_data.cmv as cmv, 
		track_data.time as time
		FROM track_track_data
		INNER JOIN track ON track.id = track_track_data.track_id
		INNER JOIN track_data ON track_data.id = track_track_data.track_data_id
		INNER JOIN user_car ON user_car.id = track.user_car_id
		INNER JOIN car ON car.id = user_car.car_id
		INNER JOIN user ON user.id = user_car.user_id
		WHERE track.id = ?", array($trackID));

	if ($statement) {

		$result = $statement -> fetchObject();

		$currentTrack = new Track();
		$currentTrack -> id = (int)$result -> id;
		$currentTrack -> date = $result -> date;

		//the user
		$currentUser = new User();
		$currentUser -> userID = (int)$result -> userID;
		$currentUser -> userName = $result -> userName;

		// the car
		$currentCar = new Car();
		$currentCar -> carID = (int)$result -> carID;
		$currentCar -> carName = $result -> carName;
		$currentCar -> carHorsePower = (int)$result -> carHorsePower;

		$trackDataArray = array();

		// extract ALL the TrackData :)
		while ($result) {
			$currentTrackData = new TrackData();
			$currentTrackData -> trackDataID = (int)$result -> trackDataID;
			$currentTrackData -> lat = $result -> lat;
			$currentTrackData -> lon = $result -> lon;
			$currentTrackData -> alt = $result -> alt;
			$currentTrackData -> consumption = floatval($result -> consumption);
			$currentTrackData -> speed = floatval($result -> speed);
			$currentTrackData -> engineLoad = floatval($result -> engineLoad);
			$currentTrackData -> engineCoolantTemperature = floatval($result -> engineCoolantTemperature);
			$currentTrackData -> engineRuntime = floatval($result -> engineRuntime);
			$currentTrackData -> airTemperature = floatval($result -> airTemperature);
			$currentTrackData -> oilTemperature = floatval($result -> oilTemperature);
			$currentTrackData -> fuelRate = floatval($result -> fuelRate);
			$currentTrackData -> cmv = floatval($result -> cmv);
			$currentTrackData -> time = $result -> time;

			array_push($trackDataArray, $currentTrackData);

			$result = $statement -> fetchObject();
		}

		// build track object
		$currentTrack -> user = $currentUser;
		$currentTrack -> car = $currentCar;
		$currentTrack -> tracks = $trackDataArray;

		// create JSON
		echo json_encode($currentTrack);
	} else {
		echo "ASDFSDF";
	}
} else {
	echo "Provided Track ID is not a number!";
}
?>